pipeline {
    agent any
    stages {
        stage('并行阶段 1') {
            parallel {
                stage('检出') {
                    steps {
                        checkout([$class           : 'GitSCM', branches: [[name: env.GIT_BUILD_REF]],
                                  userRemoteConfigs: [[url: env.GIT_REPO_URL, credentialsId: env.CREDENTIALS_ID]]])
                    }
                }
                stage('准备PIP') {
                    steps {
                        sh 'pip install pip==9.0.0'
                        sh 'pip install coscmd'
                        echo 'cosCMD准备完毕'
                    }
                }
            }
        }
        stage('并行阶段 2') {
            parallel {
                stage('构建') {
                    steps {
                        echo '构建中...'
                        sh 'mvn clean install'
                        echo '构建结束'
                    }
                }
                stage('为更新服务配置CosCMD') {
                    steps {
                        sh 'coscmd config -a AKIDsYzOYSmUdW9Xprs7PSFo9PQBhy0mtjz0 -s zkGZe5LsukBgy1pNglZ9g2yiOs54nIWT -b dice-1256090486 -r ap-chengdu'
                    }
                }
            }
        }
        stage('并行阶段 3') {
            parallel {
                stage('收集') {
                    steps {
                        archiveArtifacts(artifacts: 'target/java-1.0-SNAPSHOT-jar-with-dependencies.jar', caseSensitive: true, onlyIfSuccessful: true)
                    }
                }
                stage('合并文件包') {
                    steps {
                        sh '''\\mv target/java-1.0-SNAPSHOT-jar-with-dependencies.jar /root/workspace/SitaNyaDice/data/app/org.meowy.cqp.jcq/app/com.sinanya.dice.jar
\\cp src/main/resources/com.sinanya.dice.json /root/workspace/SitaNyaDice/data/app/org.meowy.cqp.jcq/app/com.sinanya.dice.json
'''
                        echo '合并完毕'
                    }
                }
                stage('上传到更新服务器') {
                    steps {
                        sh 'coscmd upload /root/workspace/SitaNyaDice/data/app/org.meowy.cqp.jcq/app/com.sinanya.dice.jar com.sinanya.dice.jar'
                        sh 'coscmd upload /root/workspace/src/main/resources/version version'
                        echo '上传到更新服务器完毕'
                    }
                }
            }
        }
        stage('压缩文件包') {
            steps {
                sh 'tar zcvf SinaNyaDice.tar.gz /root/workspace/SitaNyaDice/'
                echo '压缩完毕'
            }
        }
        stage('并行阶段 5') {
            parallel {
                stage('为整合包配置COSCMD') {
                    steps {
                        sh 'coscmd config -a AKIDsYzOYSmUdW9Xprs7PSFo9PQBhy0mtjz0 -s zkGZe5LsukBgy1pNglZ9g2yiOs54nIWT -b dice-1256090486 -r ap-chengdu'
                    }
                }
                stage('上传') {
                    steps {
                        script {
                            def version = readFile 'src/main/resources/version'
                            sh 'curl -T SinaNyaDice.tar.gz  -u sitanya@icloud.com:lfdy1973  "https://sinanya-generic.pkg.coding.net/SinaNya/sinanya_dice/dice?version="' + version
                        }

                    }
                }
                stage('上传到cos') {
                    steps {
                        sh 'coscmd upload SinaNyaDice.tar.gz SinaNyaDice.tar.gz'
                    }
                }
            }
        }
    }
}